#pragma once
//#include "riterator.hpp"
#include <iostream>
#include <assert.h>
#include <set>
#include <map>
using namespace std;
namespace cc
{
    template<class T>
    class vector
    {
     public:
        typedef T* iterator;
        typedef const T* const_iterator;
        vector()
            : _start(new T[4])
            , _finish(_start)
            , _end_of_strag(_start+4)
        {}
        template<class Iterator>
        vector(Iterator begin,Iterator end)
            : _start(new T[4])
            , _finish(_start)
            , _end_of_strag(_start+4)
        {
            //同样，这里最好不要用memcpy等函数(涉及深浅拷贝问题)
            while(begin!=end)
            {
                push_back(*begin);
                begin++;
            }
        }
        vector(const vector<T>& d)
            : _start(nullptr)
            , _finish(nullptr)
            , _end_of_strag(nullptr)
        {
            vector<T> tem(d.begin(),d.end());
            swap(tem);
        }
        ~vector()
        {
            delete[] _start;
            _start=_finish=_end_of_strag=nullptr;
        }
        vector<T>& operator=(vector<T> d)
        {
            swap(d);
            return *this;
        }
        void swap(vector<T>& d)
        {
            std::swap(_start,d._start);
            std::swap(_finish,d._finish);
            std::swap(_end_of_strag,d._end_of_strag);
        }
        iterator begin()
        {
            return _start;
        }
        iterator end()
        {
            return _finish;
        }
        const_iterator begin()const
        {
            return _start;
        }
        const_iterator end()const
        {
            return _finish;
        }
        size_t size()const
        {
            return _finish-_start;
        }
        size_t capacity()const
        {
            return _end_of_strag-_start;
        }
        T& operator[](size_t x)const
        {
            assert(x<size());
            return _start[x];
        }
        void resize(size_t n,const T& x=T())
        {
            _finish=_start+n;
            for(size_t i=0;i<n;i++)
                _start[i]=x;
        }
        //正确版
        void reserve(size_t n)
        {
            if(n>size())
            {
                iterator tem=new T[n];
                //这里最好不要用memcpy等函数(涉及深浅拷贝问题)
                for(size_t i=0;i<size();i++)
                    tem[i]=_start[i];
                size_t ret=_finish-_start;
                delete[] _start;
                _start=tem;
                _end_of_strag=_start+n;
                _finish=_start+ret;
            }
        }
        //错误版
        // void reserve(size_t n)
        // {
        //     if(n>size())
        //     {
        //         iterator tem=new T[n];
        //         for(size_t i=0;i<size();i++)
        //             tem[i]=_start[i];
        //         delete[] _start;
        //         _start=tem;
        //         //经典错误出现的地方
        //         _finish=_start+size();
        //         _end_of_strag=_start+n;
        //     }
        // }
        void push_back(const T& x)
        {
            if(size()==capacity())
                reserve(2*capacity());
            _start[_finish-_start]=x;
            _finish++;
        }        
     private:
        iterator _start;
        iterator _finish;
        iterator _end_of_strag;  
    };
    class iterator
    {

    };
}